<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客正文页</title>
    <link rel="stylesheet" href="css/blog_common.css">
    <link rel="stylesheet" href="css/blog_content.css">
</head>
<body>
 <!-- 导航栏部分 -->
    <div class="nav">
        <img src="image/img_1.jpg" alt="">
        <span class="title">我的博客系统</span>
        <div class="space"></div>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout">注销</a>
    </div>

    <!-- 页面主体部分 -->
    <div class="container">
        <!-- 左侧板块信息 -->
        <div class="container-left">
            <!-- 使用card 表示用户信息 -->
            <div class="card">
                <!-- 用户头像 -->
                <img src="image/img_1.jpg" alt="">
                <!-- 用户名 -->
                <h3></h3>
                <a href="#">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span class="blog_count"></span>
                    <span>1</span>
                </div>
            </div>
            <div class="function">

            </div>
        </div>
        <!-- 右侧板块信息 -->
        <div class="container-right">
            <!-- 博客标题 -->
            <h3 class="tittle"></h3>
            <!-- 博客发布时间 -->
            <div class="date"></div>
            <!-- 博客正文 -->
            <div id="content">
                
            </div>
        </div>
    </div>

    <script src="js/jqurery.min.js"></script>
    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="editor.md/editormd.js"></script>

    <script>
        $.ajax({
            method: 'get',
            url: 'blog' + location.search,
            success: function(body) {
                let tittleDiv = document.querySelector('.container-right .tittle');
                tittleDiv.innerHTML = body.tittle;

                let dateDiv = document.querySelector('.date');
                dateDiv.innerHTML = body.postTime;

                editormd.markdownToHTML('content',{markdown: body.content});
                
            }
        });

        function checkLogin(){
            $.ajax({
                method: "get",
                url: "login",
                success: function(body){
                    if(body.userId && body.userId > 0){
                        console.log("当前用户已经登录！");
                    }else{
                        location.assign("blog_login.html");
                    }
                }
            });
        }

        checkLogin();

        function getAuthor(){
            $.ajax({
                method: 'get',
                url: "author" + location.search,
                success: function(body){
                    let h3 = document.querySelector('.container-left .card h3');
                    h3.innerHTML = body.username;
                    if(body.same){
                        let functionDiv = document.querySelector(".container-left .function");
                        let delDiv = document.createElement("div");
                        let delBtn = document.createElement("a");
                        delBtn.innerHTML = "删除博客";
                        delBtn.href = "#";
                        delBtn.onclick = deleteBlog;
                        delDiv.appendChild(delBtn);
                        functionDiv.appendChild(delDiv);

                        let updateDiv = document.createElement("div");
                        let updateBtn = document.createElement("a");
                        updateBtn.innerHTML = "修改博客";
                        updateBtn.href = "#";
                        updateBtn.onclick = updateBlog;
                        updateDiv.appendChild(updateBtn);
                        functionDiv.appendChild(updateDiv);
                    }
                }
            });
        }

        getAuthor();

        function deleteBlog(){
            $.ajax({
                method: 'delete',
                url: 'delete' + location.search,
                success: function (body){
                    location.assign("blog_list.html");
                }

            });
        }

        function updateBlog(){
            $.ajax({
                method: 'get',
                url: 'update' + location.search,
                success: function (body){
                    let bodyTemp = JSON.parse(body);
                    location.assign("blog_edit.html?blogId="+bodyTemp.blogId);
                }
            });
        }

        function getCount(){
            $.ajax({
                method: "get",
                url: "counter" + location.search,
                success: function(body){
                    // console.log("counter111:" + body);
                    // console.log("counter111:" + body.counter);
                    let bodyTemp = JSON.parse(body);
                    // console.log("bodyTemp111:" + bodyTemp);
                    // console.log("bodyTemp111:" + bodyTemp.counter);
                    let blog_count = document.querySelector('.container-left .card .counter .blog_count');
                    blog_count.innerHTML = bodyTemp.counter;
                }
            });
        }

        getCount();

    </script>
</body>
</html>